package com.woniuxy.ams.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.ams.entity.*;
import com.woniuxy.ams.service.ICompanyInspectListService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 公司考核列表controller
 */
@RestController
@RequestMapping("/inspectList")
public class CompanyInspectListController {

    /**
     * 注入service
     */
    @Resource
    ICompanyInspectListService companyInspectListServiceImpl;

    /**
     * 新增公司考核列表
     * @param templateId
     * @param creatorId
     * @return ResultEntity
     */
    @RequestMapping("/addInspectList")
    public ResultEntity<?> addInspectList(@RequestParam("templateId") Integer templateId,
                                       @RequestParam("creatorId") Integer creatorId){
        //根据UUID随机生成考核单号，并和模板实例与创建人实例放入公司考核列表实例里
        String appraisalNumber = UUID.randomUUID().toString();
        CompanyTemplate template = new CompanyTemplate(templateId, null, null,
                null, null, null, null);
        User creator = new User();
        creator.setId(creatorId);
        //目前暂时写死审核状态
        CompanyInspectList companyInspectList = new CompanyInspectList(null, "待审核",
                appraisalNumber, template, creator, null, null,null,null,null,"待评分");
        int rows = companyInspectListServiceImpl.addCompanyInspectList(companyInspectList);
        if (rows != 0){
            return new ResultEntity<>(200, "新增成功", null);
        }else {
            return new ResultEntity<>(400, "新增失败", null);
        }
    }

    /**
     * 查询所有考核列表
     * @return ResultEntity
     */
    @RequestMapping("/findAllInspectList")
    public ResultEntity<PageInfo> findAllInspectList(@RequestBody CompanyInspectPageRequest pageRequest){
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getLimit());
        List<CompanyInspectList> companyInspectLists =
                companyInspectListServiceImpl.findAllCompanyInspectList(pageRequest.getAppraisalNo(),pageRequest.getStatus());
        PageInfo<?> pageInfo = new PageInfo<>(companyInspectLists);
        if (companyInspectLists.size() != 0){
            return new ResultEntity<PageInfo>(200, "查询成功", pageInfo);
        }else {
            return new ResultEntity<PageInfo>(400, "查询失败", null);
        }

    }

    /**
     * 更改考核列表
     * @param status
     * @param templateId
     * @param updateId
     * @return ResultEntity
     */
    @RequestMapping("/updateInspectList")
    public ResultEntity<?> updateInspectList(@RequestParam("id") Integer id,
                                          @RequestParam(value = "status",required = false,defaultValue = "") String status,
                                          @RequestParam(value = "templateId",required = false,defaultValue = "") Integer templateId,
                                          @RequestParam("updateId") Integer updateId){
        CompanyTemplate template = new CompanyTemplate(templateId, null, null,
                null, null, null, null);
        User user = new User();
        user.setId(updateId);
        CompanyInspectList companyInspectList =
                new CompanyInspectList(id, status, null, template,
                        null, null, user, null,null,null,null);
        int rows = companyInspectListServiceImpl.updateInspectList(companyInspectList);
        if (rows != 0){
            return new ResultEntity<>(200, "修改成功", null);
        }else {
            return new ResultEntity<>(400, "修改失败",null);
        }

    }


}
